/*
 * Copyright (c) 2010 All Inclusive Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.allinclusive.fw.web.struts.interceptor;

import java.text.MessageFormat;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

/**
 * 
 * @author xyg
 *
 */
public class LoggingActionInterceptor extends MethodFilterInterceptor {

    /**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 738414089087306512L;

	/**
	 * logインスタンス
	 */
	private static final Log log = LogFactory.getLog(LoggingActionInterceptor.class);
    
	/**
	 * 機能実行ログ出力時のフォーマット
	 * プロプティファイルから？
	 */
    protected static final MessageFormat format = new MessageFormat("セッションID:{0} 利用者ID:{1} 機能ID:{2}(メソッド:{3}) " );
    
    /**
     * 操作ログを出力します。
     */
	@Override
	public String doIntercept(ActionInvocation invocation) throws Exception {
		// セッションID
		HttpSession session = ServletActionContext.getRequest().getSession(true);
		String sessionId = session.getId();
		// 利用者ID　（セッションから）
		String riyoshaId = null;
		// 機能ID
		String functionId = StringUtils.stripEnd(invocation.getAction().getClass().getSimpleName().toUpperCase(), "ACTION");
		// メッソドID
		String methodName = invocation.getProxy().getMethod();
		
        // 機能実行ログを出力
        log.info(format.format( new Object[] { 
        		sessionId, 
        		riyoshaId, 
        		functionId, 
        		methodName }
        ));
        
        String result = invocation.invoke();
        return result;
	}

}
